<?php

	if(isset($_GET['add_url_cate'])){
		
	    require_once(LIB_DIR.'api/general.php');
	    require_once(LIB_DIR.'api/Snoopy.php');		
		require_once(LIB_DIR.'api/taobao/Taoapi.php'); 
		$shopDAO = GetShopDAO();
		$productDAO = GetProductDAO();		
		$cityDAO = GetCityDAO();
		$shopCategoryDAO = GetShopCategoryDAO();
		$productCategoryDAO = GetProductCategoryDAO();			
		$errmsg = GetMessageStack();

		
		$postURL=edp_db_input(trim($_GET['tburl']));	
		//判断是不是url
			
		if(!strstr($postURL,'.taobao.com')){
			//非taobao
			$_GET['result']="false";
			$_GET['text']="输入的链接地址不是淘宝店铺或商品的链接地址，添加失败，请确认后重新添加";		
			echo( json_encode($_GET) );
			return;	
		}
		
		
		if( $_GET['add_url_cate'] == "shop" )
		{
		    require_once(CONFIG_DIR.'reg_shop_config.php');

		    //通过url得到extID - http://shop*****.taobao.com
		    $extID = GetTaobaoShopIDByURL($postURL);		    
		    if(!edp_is_empty_string($extID)){		    	
		    	$shopInfo = $shopDAO -> GetShopInfoPlusByExtID('taobao',$extID);		    	
		     	if($shopInfo)
				{		 
					//已经存在数据库
					$_GET['id']=$shopInfo['id'];
					$_GET['name']=$shopInfo['shop_name'];
					$_GET['result']="true";
					$_GET['text']="成功添加店铺至评单";		
					echo( json_encode($_GET) );
					return;												     		
				}			
		    }
		    
		    
		    //通过html或取nickname	 			
			$snoopy = new Snoopy;  
 			$snoopy->fetch($postURL);	 	 			 			 
	 		$html_content=$snoopy->results;	 
	 		$isTaobaoMall = CheckRegStatus($html_content,TAOBAO_MALL); //根据正则获取关键数据nickName,淘宝商城和普通店铺					 	 	 			 	 	
	 		$nickName = ConvertEncoding(GetDataByRegx($html_content,$isTaobaoMall?TAOBAO_MALL_NICK_NAME:TAOBAO_NICK_NAME),TAOBAO_ENCODING);
	 		
	 		if(!edp_is_empty_string($nickName)){	 
		 			
	 			$shopInfo = $shopDAO->GetShopInfoByNick($nickName); //以后增加nick索引	
 			 		                
 			 	if($shopInfo)
				{		 
					//已经存在数据库
					$_GET['id']=$shopInfo['id'];
					$_GET['name']=$shopInfo['shop_name'];
					$_GET['result']="true";
					$_GET['text']="成功添加店铺至评单";		
							     		
				}else{
					
					//不存在数据库			
					$Taoapi = new Taoapi;
					$Taoapi->method = 'taobao.shop.get';
					$Taoapi->fields = 'sid,cid,nick,title,desc,bulletin,pic_path,created,modified';
					$Taoapi->nick = $nickName;
					$TaobaoData = $Taoapi->Send('get','xml')->getArrayData();
					
					 if(edp_is_empty_string($Taoapi->getErrorInfo())&&isset($TaobaoData['shop']['sid']))						 
				    {	
				    	
			       	    $extShopID    = $TaobaoData['shop']['sid']; //taobao平台的扩展id，作为shop_info_taobao的主键
					    $shopName     = $TaobaoData['shop']['title'];
					    $createdDate  = substr($TaobaoData['shop']['created'],0,10);
					    $shopDesc  = is_array($TaobaoData['shop']['desc'])?$TaobaoData['shop']['desc'][0]:$TaobaoData['shop']['desc'];		
					    $shopCategory = $shopCategoryDAO->ConvertTaobaoShopCid($TaobaoData['shop']['cid']);	 //把taobaoAPI的cid转换为本地的cid,	return 0 if no value				    					    			 			 					 		
					    $shopLocation = $cityDAO->GetCityID(ConvertEncoding(GetDataByRegx($html_content,$isTaobaoMall?TAOBAO_MALL_LOCATION:TAOBAO_LOCATION),TAOBAO_ENCODING));//把正则获取的location称转换为本地的city id, return 0 if no value					 						
				 		$city = $cityDAO->GetCityNameSimple($shopLocation); 				 		
						$provinceid = ($cityDAO->GetProvinceIDByCityID($shopLocation)!=0)?$cityDAO->GetCityNameSimple( $provinceid ):$city; 							      
					
			            if($isTaobaoMall){	
			            	$isMall = '1';				    	
					    	$logoURL = TAOBAO_MALL_LOGO; //淘宝商城店铺统一logo
				        }else{
			            	$isMall = '0';	
				         	if(isset($TaobaoData['shop']['pic_path']))
				         		$logoURL = TAOBAO_PIC_PREFIX.$TaobaoData['shop']['pic_path'];
						}
						
						$Taoapi->method = 'taobao.taobaoke.shops.convert';
						$Taoapi->fields = 'user_id,shop_title,click_url,shop_commission.rate';
						$Taoapi->sids = $extShopID;
						$Taoapi->nick = TAOBAOKE_ID;
						$TaobaokeData = $Taoapi->Send('get','xml')->getArrayData();
											
						if(edp_is_empty_string($Taoapi->getErrorInfo()) && !empty($TaobaokeData)){	
					    	$commissionURL = $TaobaokeData['taobaoke_shops']['taobaoke_shop']['click_url'];
					    	$commissionRate = $TaobaokeData['taobaoke_shops']['taobaoke_shop']['shop_commission.rate'];					    	
					    }else{
					        $commissionRate = '0';   
						    $commissionURL =  $postURL;  
					    }
					       				
						$shopID = $shopDAO->InsertTaobaoShopData('1',$shopName,$postURL,$logoURL,
													    $nickName,$createdDate,$shopLocation,GetLoginUserID(),
														'1',$shopCategory,$shopDesc,$extShopID,$commissionURL,$commissionRate,$isMall);										

				    	if(!edp_is_empty_string($shopID)){
					   		
							$_GET['id']=$shopID;
							$_GET['name']=$shopName;
							$_GET['result']="true";
							$_GET['text']="成功添加店铺至评单";	
					   	
					   	}else{
							$_GET['result']="false";
							$_GET['text']="出现异常，请重新添加";						   				 
					   	}													    
						
				    
				    }else{
				    	
						$_GET['result']="false";
						$_GET['text']="获取数据失败，请重新添加";					    	
					}	
				}//end of add process(if判断是否存在，else不存在则添加)
				
	 		}else{//end of check nick

				$_GET['result']="false";
				$_GET['text']="获取店铺数据失败，请确认店铺链接地址是否正确，然后重新添加";				
	 		}	

	 		
		////////end of shop//////////	
	 		
	 		
		}else if ($_GET['add_url_cate'] == "product"){
			
			require_once(CONFIG_DIR.'reg_product_config.php');			
			
			//通过url获取extID
 			$extID = GetTaobaoProductIDByURL($postURL);
	 						 				
 			if(edp_is_empty_string($extID)){

 				//通过url得不到，然后通过html获取extID
				$snoopy = new Snoopy;  
	 		    $snoopy->fetch($postURL);	 	 			 			 
	 			$html_content=$snoopy->results;		 						 
				$extID = GetTaobaoProductID($html_content);	 	
 			}

 			if(!edp_is_empty_string($extID)){
 			
	 			//根据extID检索商品是否已存在			
	 			$productInfo = $productDAO->GetProductInfoPlusByExtID('taobao',$extID);
	 			
		     	if($productInfo)
				{		 
					$_GET['id']=$productInfo['id'];
					$_GET['name']=$productInfo['product_name'];
					$_GET['result']="true";
					$_GET['text']="成功添加商品至评单";		
					echo( json_encode($_GET) );
					return;												     		
				
				}else{					 	
		                        
					$Taoapi = new Taoapi;
					$Taoapi->method = 'taobao.taobaoke.items.detail.get';
					$Taoapi->fields = 'nick,num_iid,title,cid,pic_url,location,price,click_url';// remove product desc by bo
					$Taoapi->num_iids = $extID; 
					$Taoapi->nick = TAOBAOKE_ID;
					$TaobaoData = $Taoapi->Send('get','xml')->getArrayData();
					$tbData = $TaobaoData['taobaoke_item_details']['taobaoke_item_detail'];
					
					if(edp_is_empty_string($Taoapi->getErrorInfo())&&isset($tbData['item']['nick']))						 
					{	
	
						$shopInfo = $shopDAO->GetShopInfoByNick($tbData['item']['nick']);
						
					    if(!$shopInfo){
							$_GET['result']="false";
							$_GET['text']="该商品所属的淘宝店铺不存在，请先添加该店铺至花开里，<a href='http://www.huakaili.com/add_shop'>点击添加</a>";		
							echo( json_encode($_GET) );
							return;			
	                    }
	
	                    $productName = $tbData['item']['title'];
						$productPicture = $tbData['item']['pic_url'];
						$productPrice = $tbData['item']['price'];					        
						$productCategory = $productCategoryDAO->ConvertTaobaoProductCid($tbData['item']['cid']);  						        
						$productCategoryName = $productCategoryDAO->GetProductCategoryName($productCategory);  							
					    $productLocation = $cityDAO->GetCityID($tbData['item']['location']['state'].$tbData['item']['location']['city']);	
					 	$city = $cityDAO->GetCityNameSimple($productLocation); 
					 	$provinceid = $cityDAO->GetProvinceIDByCityID($productLocation);
						$province = ($provinceid!=0)?$cityDAO->GetCityNameSimple( $provinceid ):$city; 							
						    						        
					    $productDetailURL = 'http://item.taobao.com/item.htm?id='.$tbData['item']['num_iid']; //存入商品规范的URL
					      
					    if(empty($tbData['click_url']) || is_array($tbData['click_url'])){
					        	$clickURL = $productDetailURL;					        	
					    }else{
					        	$clickURL = $tbData['click_url'];
					    }
		
						$productID = $productDAO->InsertTaobaoProductData($productCategory,$shopInfo['id'],$productName,$productDetailURL, $productPicture, 
														 $productLocation, $productPrice, '', '1', GetLoginUserID(), $extID,
														 $clickURL,'0' );	
														 			    
					   	if(!edp_is_empty_string($productID)){
					   		
							$_GET['id']=$productID;
							$_GET['name']=$productName;
							$_GET['result']="true";
							$_GET['text']="成功添加商品至评单";	
					   	
					   	}else{
							$_GET['result']="false";
							$_GET['text']="出现异常，请重新添加";						   				 
					   	}
	                  	
	                }else{
					    	
						$_GET['result']="false";
						$_GET['text']="获取数据失败，请重新添加";					    	
					}
					
				}//end of add process(if判断是否存在，else不存在则添加)
			
			}else{//end of check extID

				$_GET['result']="false";
				$_GET['text']="获取商品数据失败，请确认商品链接地址是否正确，然后重新添加";				
	 		}	
		
			///////end of product//////////	

	 		
		
		}else{	
			
			$_GET['result']="false";
			$_GET['text']="非法请求，添加失败,请重新添加";
		}
	
		echo( json_encode($_GET) );
		
	}//end of if if(isset($_GET['add_url_cate']))

?>